/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.paris5.master2.guitarmaster.bean.EcomCountry;
import com.paris5.master2.guitarmaster.bean.EcomScore;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceCustomerRemote;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Leticia
 */
public class UserCreation extends EcomAction {
    private String lastName;
    private String firstName;
    private String password;
    private String adresse;
    private String country;
    private String cellular;
    private String phone;
    private String email;

    @Override
    public String execute()
    {
        ServiceCustomerRemote serviceCustomerRemote = lookupServiceCustomerRemote();
        EcomUser ecomUser = new EcomUser();
        ecomUser.setLastName(lastName);
        ecomUser.setFirstName(firstName);
        ecomUser.setPassword(password);
        ecomUser.setAdresse(adresse);
        EcomCountry ecomCountry = new EcomCountry();
        ecomCountry.setLabel(country);        
        ecomCountry.setCode(getCode(country));
        ecomUser.setCountry(ecomCountry);
        ecomUser.setCellular(cellular);
        ecomUser.setPhone(phone); 
        ecomUser.setEmail(email);
        ecomUser.setCreateDate(new Date());
        ecomUser.setBalance(100);   
        ecomUser.setScore(new EcomScore(0, 0, 0));
        try {
            serviceCustomerRemote.subscribe(ecomUser);
            return SUCCESS;
        } catch (EcomValidationException ex) {
            Logger.getLogger(UserCreation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomQueryException ex) {
            Logger.getLogger(UserCreation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomException ex) {
            setMessage("L'utilisateur existe déjà!");
        }
        return INPUT;
    }
    

    @RequiredStringValidator(message = "Nom requis.")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @RequiredStringValidator(message = "Prénom requis.")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @RequiredStringValidator(message = "Mont de passe requis.")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @RequiredStringValidator(message = "Adresse requise.")
    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    @RequiredStringValidator(message = "Pays requis.")
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
    
    @StringLengthFieldValidator(message="Le nombre de caractères doit être entre 1 et 10",  minLength = "1",  maxLength = "10")
    public String getCellular() {
        return cellular;
    }

    public void setCellular(String cellular) {
        this.cellular = cellular;
    }

    @StringLengthFieldValidator(message="Le nombre de caractères doit être entre 1 et 10",  minLength = "1",  maxLength = "10")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "email",
    message = "Veuillez saisir un email valide.")
    @RequiredStringValidator(message = "Adresse e-mail requise.")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }  
    
    private String getCode(String country) {
        if (country.equals("USA")){
            return "EN";
        }
        return "FR";
    }
}
